/**
 * @author zyj
 * @date 2022年06月09日 9:19
 */
public class VolatileDemo {
    public static void main(String[] args) {
       ThreadA a = new ThreadA();
       a.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        a.flag = false;
        System.out.println("赋值为false!");
    }

}

class ThreadA extends Thread{
    public volatile boolean flag = true;
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "执行开始");
       while (flag){

       }
       System.out.println(Thread.currentThread().getName() + "执行结束");
    }


}

